package worldinruin.gui.imp.gameview;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.util.HashMap;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.border.EtchedBorder;
import javax.swing.border.LineBorder;

import worldinruin.core.IGame;
import worldinruin.core.IPlayer;
import worldinruin.core.imp.ResourceTypes;
import worldinruin.core.imp.buildings.City;
import worldinruin.gui.imp.PanelFactory;
import worldinruin.gui.imp.gameview.gameboard.tiles.MapPanel;
import worldinruin.resources.IconMap;
import worldinruin.resources.Panels;
import worldinruin.utils.ResizableLabel;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class GameViewMainPanel extends javax.swing.JPanel {
	private JSplitPane leftPanelSplitPane;
	private JSplitPane rightTopPanelSplitPane;
	private JSplitPane rightBottomPanelSplitPane;
	private JPanel leftCardStack;
	private CardLayout leftCardStackLayout;
	private CardLayout rightCardStackLayout;
	private MapPanel mapPanel;
	private JPanel playerResourcesPanel;
	
	private static GameViewMainPanel instance;
	
	private int numberOfRows;
	private int numberOfColumns;
	private JScrollPane leftScrollPane;
	
	private SelectedBuildingPanel selectedBuildingPanel;
	private MessagePanel messagePanel;
	private JLabel logo;
	private JPanel rightCardStack;
	private HashMap<IPlayer, ResourcePanel> playerResourcePanelMap = new HashMap<IPlayer, ResourcePanel>();
	
	private IGame game;
	
	
	/**
	 * Creates a game view main panel.
	 * @param game Game to display on the panel.
	 */
	public GameViewMainPanel(IGame game) {
		instance = this;
		this.game = game;
		initGUI();
	}
	
	// Since there are two separate card stacks in the class, they are separated by
	// 1 for the left hand card stack and 2 for the right hand card stack.
	
	/**
	 * Show panel on game view main panel.
	 * @param cardStack Card stack on main panel.
	 * @param panel Panel to show.
	 * @param o Updating objects.
	 */
	public void showPage(Panels cardStack, Panels panel, Object o) {
		PanelFactory factory = new PanelFactory();
		showPage(cardStack, panel.toString(), factory.createPanel(panel, o));
		
	}
	
	/**
	 * Show panel on game view main panel.
	 * @param cardStack Card stack on main panel.
	 * @param panel Panel to show.
	 */
	public void showPage(Panels cardStack, Panels panel) {
		PanelFactory factory = new PanelFactory();
		
		showPage(cardStack, panel.toString(), factory.createPanel(panel));
		
	}
	
	/**
	 * Show panel on game view main panel.
	 * @param cardStack Card stack on main panel.
	 * @param card Name of the card.
	 * @param panel Panel to show.
	 */
	public void showPage(Panels cardStack, String card,  JPanel panel) {
		if (cardStack == Panels.GAMEVIEWLEFTCARDSTACK) {
			leftCardStack.removeAll();
			leftCardStack.add(panel, card);
			leftCardStackLayout.show(leftCardStack, card);
			revalidate();
		}
		else {
			rightCardStack.removeAll();
			rightCardStack.add(panel, card);
			rightCardStackLayout.show(rightCardStack, card);
			revalidate();
		}
	}
	
	/**
	 * Initializes the GUI.
	 */
	private void initGUI() {
		try {
			GridLayout thisLayout = new GridLayout(1, 1);
			thisLayout.setHgap(5);
			thisLayout.setVgap(5);
			thisLayout.setColumns(1);
			setPreferredSize(new Dimension(400, 300));
			this.setLayout(thisLayout);
			{
				final JSplitPane gameViewSplitPane = new JSplitPane();
				this.add(gameViewSplitPane);
				gameViewSplitPane.setResizeWeight(0.2);
				{
					leftPanelSplitPane = new JSplitPane();
					gameViewSplitPane.add(leftPanelSplitPane, JSplitPane.LEFT);
					leftPanelSplitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
					leftPanelSplitPane.setResizeWeight(0.2);
					{
						leftScrollPane = new JScrollPane();
						leftPanelSplitPane.add(leftScrollPane, JSplitPane.RIGHT);
						{
							leftCardStack = new JPanel();
							leftCardStackLayout = new CardLayout();
							leftCardStack.setLayout(leftCardStackLayout);
							leftScrollPane.setViewportView(leftCardStack);
							{
								selectedBuildingPanel = new SelectedBuildingPanel(new City());
								leftCardStack.add(selectedBuildingPanel, "selectedBuildingPanel");
							}
						}
					}
					{
						logo = new ResizableLabel(IconMap.getInstance().getIcon(ResourceTypes.ICON).getImage());
						FlowLayout logoLayout = new FlowLayout();
						logo.setLayout(logoLayout);
						logo.setMinimumSize(new Dimension(300, 90));
						leftPanelSplitPane.add(logo, JSplitPane.TOP);

					}
				}
				{
					rightTopPanelSplitPane = new JSplitPane();
					gameViewSplitPane.add(rightTopPanelSplitPane, JSplitPane.RIGHT);
					rightTopPanelSplitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
					rightTopPanelSplitPane.setResizeWeight(0.2);
					{
						playerResourcesPanel = new JPanel();
						GridLayout playerResourceLayout = new GridLayout((game.getPlayers().size() + 1) / 2, 2);
						playerResourcesPanel.setLayout(playerResourceLayout);
						for(IPlayer player : game.getPlayers()) {
							ResourcePanel resourcePanel = new ResourcePanel(player);
							resourcePanel.setBackground(player.getColor());
							playerResourcePanelMap.put(player, resourcePanel);
							playerResourcesPanel.add(resourcePanel);
						}
						game.setActivePlayer(game.getPlayers().get(0));
						playerResourcePanelMap.get(game.getActivePlayer()).setBorder(new LineBorder(Color.YELLOW, 2));
						playerResourcesPanel.setPreferredSize(new java.awt.Dimension(500, 100));
						rightTopPanelSplitPane.add(playerResourcesPanel, JSplitPane.TOP);
					}
					{
						rightBottomPanelSplitPane = new JSplitPane();
						rightTopPanelSplitPane.add(rightBottomPanelSplitPane, JSplitPane.BOTTOM);
						rightBottomPanelSplitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
						rightBottomPanelSplitPane.setResizeWeight(0.8);
						{

							rightCardStack = new JPanel();
							rightCardStackLayout = new CardLayout();
							rightCardStack.setLayout(rightCardStackLayout);
							rightBottomPanelSplitPane.add(rightCardStack, JSplitPane.TOP);
							rightCardStack.setPreferredSize(new java.awt.Dimension(550, 550));
							{
								mapPanel = new MapPanel(game.getMap());
								rightCardStack.add(mapPanel, Panels.MAPPANEL.toString());
								numberOfRows = game.getMap().getNumberOfRows();
								numberOfColumns = game.getMap().getNumberOfColumns();
								GridLayout mapPanelLayout = new GridLayout(numberOfRows, numberOfColumns);
								mapPanelLayout.setColumns(1);
								mapPanel.setLayout(mapPanelLayout);
								mapPanel.setPreferredSize(new java.awt.Dimension(500, 400));
							}
						}
						{
							messagePanel = new MessagePanel("New game started");
							rightBottomPanelSplitPane.add(messagePanel, JSplitPane.BOTTOM);
							messagePanel.setPreferredSize((new java.awt.Dimension(500, 30)));
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * When a player ends his or her turn, the next player in the list gets his or her
	 * resourcepanel highlighted.
	 */
	public void setActivePlayerResourcePanel() {
		playerResourcePanelMap.get(game.getActivePlayer()).setBorder(new EtchedBorder());
		game.setActivePlayer();
		playerResourcePanelMap.get(game.getActivePlayer()).setBorder(new LineBorder(Color.YELLOW, 2));
	}
	
	/**
	 * Updating messages.
	 * @param text Message to users.
	 */
	public void updateMessage(String text) {
		messagePanel.updateMessage(text);
	}

	/**
	 * This method is called if other objects needs access to this class.
	 * @return Single instance of GameViewMainPanel.
	 */
	public static GameViewMainPanel getInstance() {
		return instance;
	}

}
